<script setup>
// 保持原有逻辑不变
import { reactive, ref,onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { Lock, Key, User } from '@element-plus/icons-vue';  // 引入User图标
import loginApi from '../api/login_api.js'

const manager = ref({
  manager_id: '',
  log_code: '',
  captcha: '',
  key: ''
});
onMounted(function() {
  // 先展示干掉，测试一下
  // localStorage.removeItem("token");

  loadCaptcha();
})

const router = useRouter();
const captcha = ref("");

const onSubmit = async () => {
  try {
    const res = await loginApi.dologin(manager.value);
    if (res) {
      localStorage.setItem("token", res);
      ElMessage.success('登录成功，正在跳转...');
      router.push('/index');
    }
  } catch (error) {
    ElMessage.error(error.message || '登录失败，请检查账号密码或验证码');
    loadCaptcha();
  }
}

// 加载验证码
const loadCaptcha = async () => {
  try {
    const res = await loginApi.captcha();
    manager.value.key = res.key;
    captcha.value = res.base;
  } catch (error) {
    ElMessage.error('验证码加载失败，请重试');
  }
}

// 新增：跳转到注册页面的方法
const goToRegister = () => {
  router.push('/register');
}

</script>


<template>
  <div class="login-container">
    <!-- 动图背景 -->
    <div class="bg-gif"></div>

    <!-- 登录卡片（居中布局） -->
    <el-form class="login-card" :model="manager">
      <div class="login-header">
        <img src="@/assets/logo.png" alt="抱抱窝" class="login-logo"/>
        <h1 class="login-title">抱抱窝管理系统</h1>
      </div>

      <el-form-item>
        <el-input
            v-model="manager.manager_id"
            placeholder="请输入手机号"
            :prefix-icon="User"
        maxlength="16"
        class="custom-input"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-input
            v-model="manager.log_code"
            placeholder="请输入密码"
            show-password
            :prefix-icon="Lock"
            maxlength="16"
            class="custom-input"
        ></el-input>
      </el-form-item>

      <el-form-item class="captcha-group">
        <el-input
            v-model="manager.captcha"
            placeholder="验证码"
            :prefix-icon="Key"
            maxlength="5"
            class="captcha-input"
        ></el-input>
        <img
            :src="captcha"
            class="captcha-img"
            @click="loadCaptcha"
            title="点击刷新"
        >
      </el-form-item>

      <el-form-item>
        <el-button
            class="login-btn"
            type="primary"
            @click="onSubmit"
        >
          登录
        </el-button>
      </el-form-item>

      <!-- 新增：注册按钮 -->
      <el-form-item>
        <el-button
            class="register-btn"
            type="default"
            @click="goToRegister"
        >
          注册账号
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 页脚信息 -->
    <div class="login-footer">
      © 2025 抱抱窝宠物管理系统 版权所有
    </div>
  </div>
</template>

<style scoped>
/* 基础布局 - 相对定位容器 */
.login-container {
  position: relative;
  min-height: 100vh;
  overflow: hidden; /* 隐藏超出屏幕的背景 */
}

/* 动图背景 - 铺满全屏 */
.bg-gif {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* 使用指定动图作为背景 */
  background-image: url('../assets/bb.gif');
  background-size: cover; /* 覆盖全屏 */
  background-position: center; /* 居中显示 */
  z-index: 0; /* 背景在最底层 */
}

/* 登录卡片 - 居中布局（核心修改） */
.login-card {
  position: absolute;
  /* 水平居中 */
  left: 50%;
  transform: translateX(-50%);
  /* 垂直居中（距离顶部50%减去自身一半高度） */
  top: 50%;
  transform: translate(-50%, -50%);

  width: 360px;
  padding: 36px 40px 40px;
  background: rgba(255, 255, 255, 0.7); /* 半透明白色背景 */
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  z-index: 1; /* 在背景上层 */
  backdrop-filter: blur(5px); /* 轻微毛玻璃效果 */
}

/* 头部区域 */
.login-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.login-logo {
  width: 80px;
  height: 80px;
  object-fit: contain;
  margin-bottom: 16px;
  border-radius: 50%;
  background: #fff;
  padding: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  font-size: 22px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

/* 输入框样式 */
.custom-input {
  height: 50px;
  border-radius: 8px;
  border-color: #e5e7eb;
  transition: all 0.3s ease;
}

.custom-input:hover, .custom-input:focus-within {
  border-color: #a5dee5;
  box-shadow: 0 0 0 3px rgba(165, 222, 229, 0.2);
}

/* 验证码区域 */
.captcha-group {
  display: flex;
  gap: 10px;
}

.captcha-input {
  flex: 1;
  height: 50px;
  border-radius: 8px;
}

.captcha-img {
  width: 200px;
  height: 60px;
  border-radius: 8px;
  cursor: pointer;
  object-fit: cover;
  background: #f3f4f6;
  transition: transform 0.2s ease;
}

.captcha-img:hover {
  transform: scale(1.02);
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: #a5dee5;
  border: none;
  transition: all 0.3s ease;
}

.login-btn:hover {
  background: #ffb6b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(252, 182, 217, 0.3);
}

.login-btn:active {
  transform: translateY(0);
}

/* 新增：注册按钮样式 */
.register-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid #a5dee5;
  color: #666;
  margin-top: 10px;
  transition: all 0.3s ease;
}

.register-btn:hover {
  background: #f0f7ff;
  border-color: #ffb6b9;
  color: #ff6b8b;
}

/* 页脚信息 - 底部居中显示 */
.login-footer {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%); /* 水平居中 */
  color: #ed8d8d; /* 白色文字适配动图背景 */
  font-size: 14px; /* 调整为合适大小 */
  z-index: 1;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); /* 文字阴影增强可读性 */
}

/* 响应式调整 - 小屏幕适配 */
@media (max-width: 768px) {
  .login-card {
    width: 90%;
    max-width: 360px;
    padding: 28px 24px;
  }
}
</style>
